package com.zhongli.devplatform.component;

import com.zhongli.devplatform.service.imp.sys.ReadSystemParameterService;
import jodd.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

@Component
public class FileUtil {

    @Autowired
    private ReadSystemParameterService systemParameterService;

    /**
     * 拼接前端上传的文件url
     * @param files 文件
     * @param urlName 文件服务器自定义文件夹名称
     * @return
     */
    public List<String> getFileList(String files, String urlName){
        List<String> imgList = new ArrayList<>();
        if(StringUtil.isEmpty(files)){
            return imgList;
        }
        String fileServer = systemParameterService.getSystemParameter("file:fileServer");
        for(int i = 0; i < files.split(",").length; i++){
            String image = fileServer + "/file/"+urlName+"/" + files.split(",")[i];
            imgList.add(image);
        }
        return imgList;
    }


    public String getFileStr(String files, String urlName){
        String imgStr = "";
        if(StringUtil.isEmpty(files)){
            return imgStr;
        }
        String fileServer = systemParameterService.getSystemParameter("file:fileServer");
        imgStr = fileServer + "/file/"+urlName+"/" + files;
        return imgStr;
    }

}
